//线程终止还有一种办法
//主线程可以取消线程 pthread_cancel

#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <functional>
using namespace std;

void* threadRoutine(void* args)
{
    int cnt = 5;
    while(cnt--)
    {
        cout << "thread is running..." << endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, nullptr);
    sleep(3);
    
    int n = pthread_cancel(tid); //取消tid线程
    cout << "main thread cancel done, " << " n: " << n << endl;

    void* ret = nullptr;
    n = pthread_join(tid, &ret); 
    cout << "main thread join done, " << " n: " << n  << ", thread return:  "<< (int64_t)ret << endl;
    return 0;
}